import torch
import os

class LoopOutputList:
    """列表索引输出器
    根据外部传入的索引直接输出对应位置的数据
    """
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "program_names_list": ("LIST",),
                "ip_images_list": ("LIST",),
                "bg_images_list": ("LIST",),
                "logo_images_list": ("LIST",),
                "image_names_list": ("LIST",),
                "current_index": ("INT", {"default": 0, "min": 0, "max": 9999})
            },
            "optional": {
                "reset": ("BOOLEAN", {"default": False}),
            }
        }

    RETURN_TYPES = ("STRING", "IMAGE", "IMAGE", "IMAGE", "STRING", "INT", "BOOLEAN", "LIST", "LIST", "LIST", "LIST", "LIST", "INT", "INT")
    RETURN_NAMES = ("节目名", "ip图片", "背景图片", "logo图片", "图片名称", "当前索引", "是否处理完成", "剩余节目名列表", "剩余ip图片列表", "剩余背景图片列表", "剩余logo图片列表", "剩余图片名称列表", "已处理数量", "总数量")
    FUNCTION = "loop_output"
    CATEGORY = "psd"

    def __init__(self):
        self.list_length = 0

    def loop_output(self, program_names_list, ip_images_list, bg_images_list, logo_images_list, image_names_list, current_index, reset=False):
        # 初始化列表长度
        if self.list_length == 0 and len(program_names_list) > 0:
            self.list_length = len(program_names_list)
            print(f"列表长度初始化: {self.list_length}")

        # 检查列表是否为空
        if self.list_length == 0:
            print("警告: 输入列表为空")
            # 返回空值
            empty_image = torch.zeros((1, 3, 256, 256), dtype=torch.float32)
            return ("", empty_image, empty_image, empty_image, "", -1, False, [], [], [], [], [], 0, 0)

        # 检查索引是否有效
        if current_index < 0 or current_index >= self.list_length:
            print(f"警告: 索引 {current_index} 超出范围 [0, {self.list_length-1}]")
            # 返回空值
            empty_image = torch.zeros((1, 3, 256, 256), dtype=torch.float32)
            return ("", empty_image, empty_image, empty_image, "", -1, False, [], [], [], [], [], 0, self.list_length)

        # 获取当前元素
        program_name = program_names_list[current_index]
        ip_image = ip_images_list[current_index]
        bg_image = bg_images_list[current_index]
        logo_image = logo_images_list[current_index]
        image_name = image_names_list[current_index]

        print(f"输出第 {current_index+1}/{self.list_length} 个元素: {program_name}")

        # 计算剩余元素
        remaining_index = current_index + 1
        remaining_program_names = program_names_list[remaining_index:] if remaining_index < self.list_length else []
        remaining_ip_images = ip_images_list[remaining_index:] if remaining_index < self.list_length else []
        remaining_bg_images = bg_images_list[remaining_index:] if remaining_index < self.list_length else []
        remaining_logo_images = logo_images_list[remaining_index:] if remaining_index < self.list_length else []
        remaining_image_names = image_names_list[remaining_index:] if remaining_index < self.list_length else []

        # 判断是否处理完成
        is_complete = current_index >= self.list_length - 1

        return (
            program_name,
            ip_image,
            bg_image,
            logo_image,
            image_name,
            current_index,
            is_complete,
            remaining_program_names,
            remaining_ip_images,
            remaining_bg_images,
            remaining_logo_images,
            remaining_image_names,
            current_index + 1,  # 已处理数量
            self.list_length
        )

# 注册节点
NODE_CLASS_MAPPINGS = {
    "LoopOutputList": LoopOutputList
}

NODE_DISPLAY_NAME_MAPPINGS = {
    "LoopOutputList": "📋 列表索引输出器"
}